
// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

import NIMSDK
import UIKit

@objcMembers
open class TeamHistoryMessageController: NEBaseTeamHistoryMessageController {
  override public init(teamId: String?) {
    super.init(teamId: teamId)
    tag = "TeamHistoryMessageController"
  }

  public required init?(coder: NSCoder) {
    super.init(coder: coder)
  }

  override open func setupSubviews() {
      super.setupSubviews()
      view.backgroundColor = .normalTeamBackColor
      tableView.backgroundColor = .normalTeamBackColor
      
      // 隐藏搜索
      searchTextField.isHidden = true
      cancelButton.isHidden = true
      
      cancelButton.setTitleColor(.normalTeamOrangeColor, for: .normal)
      NSLayoutConstraint.activate([
        cancelButton.topAnchor.constraint( equalTo: view.topAnchor, constant: NEConstant.statusBarHeight + 7),
        cancelButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0),
        cancelButton.widthAnchor.constraint(equalToConstant: 58),
        cancelButton.heightAnchor.constraint(equalToConstant: 30),
      ])
      
      
      searchTextField.backgroundColor = UIColor.white
      NSLayoutConstraint.activate([
        searchTextField.topAnchor.constraint( equalTo: view.topAnchor, constant: NEConstant.statusBarHeight + 7),
        searchTextField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: kScreenInterval),
        searchTextField.rightAnchor.constraint(equalTo: cancelButton.leftAnchor, constant: 0),
        searchTextField.heightAnchor.constraint(equalToConstant: 30),
      ])
      
      
      NSLayoutConstraint.activate([
        searchTitleLabel.topAnchor.constraint( equalTo: searchTextField.bottomAnchor, constant: 57),
        searchTitleLabel.leftAnchor.constraint(equalTo: view.leftAnchor, constant: kScreenInterval),
        searchTitleLabel.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -kScreenInterval),
        searchTitleLabel.heightAnchor.constraint(equalToConstant: 18),
      ])
         
      
      NSLayoutConstraint.activate([
        fileButton.topAnchor.constraint( equalTo: searchTitleLabel.bottomAnchor, constant: 16),
        fileButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        fileButton.widthAnchor.constraint(equalToConstant: (UIScreen.main.bounds.size.width-46)/3),
        fileButton.heightAnchor.constraint(equalToConstant: 36),
      ])
      
      NSLayoutConstraint.activate([
        memberButton.topAnchor.constraint( equalTo: fileButton.topAnchor),
        memberButton.rightAnchor.constraint(equalTo: fileButton.leftAnchor),
        memberButton.widthAnchor.constraint(equalTo: fileButton.widthAnchor),
        memberButton.heightAnchor.constraint(equalTo: fileButton.heightAnchor),
      ])
        
      NSLayoutConstraint.activate([
        imageButton.topAnchor.constraint( equalTo: fileButton.topAnchor),
        imageButton.leftAnchor.constraint(equalTo: fileButton.rightAnchor),
        imageButton.widthAnchor.constraint(equalTo: fileButton.widthAnchor),
        imageButton.heightAnchor.constraint(equalTo: fileButton.heightAnchor),
      ])
      
      NSLayoutConstraint.activate([
        linkButton.topAnchor.constraint( equalTo: fileButton.bottomAnchor),
        linkButton.leftAnchor.constraint(equalTo: memberButton.leftAnchor),
        linkButton.widthAnchor.constraint(equalTo: fileButton.widthAnchor),
        linkButton.heightAnchor.constraint(equalTo: fileButton.heightAnchor),
      ])
      
      NSLayoutConstraint.activate([
        tableView.rightAnchor.constraint(equalTo: view.rightAnchor),
        tableView.leftAnchor.constraint(equalTo: view.leftAnchor),
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        tableView.topAnchor.constraint(equalTo: searchTextField.bottomAnchor, constant: 19),
      ])

      tableView.register(
        HistoryMessageCell.self,
        forCellReuseIdentifier: "\(NSStringFromClass(HistoryMessageCell.self))"
      )
      
  }
    
    override open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(
        withIdentifier: "\(NSStringFromClass(HistoryMessageCell.self))",
        for: indexPath
      ) as! NEBaseHistoryMessageCell
      let cellModel = viewModel.searchResultInfos?[indexPath.row]
      cell.searchText = searchStr
      cell.configData(message: cellModel)
        cell.backgroundColor = UIColor.white
        cell.backgroundView?.backgroundColor = UIColor.white
        cell.contentView.backgroundColor = UIColor.white
      return cell
    }
    
    open override func searchImage() {
        let controller = TeamHistoryImageMessageController(teamId: teamId)
        navigationController?.pushViewController(controller, animated: true)
    }
    
    open override func searchFile() {
        let controller = TeamHistoryFileMessageController(teamId: teamId)
        navigationController?.pushViewController(controller, animated: true)
    }
    
    open override func searchMember() {
        let controller = TeamHistoryUserMessageController(teamId: teamId)
        navigationController?.pushViewController(controller, animated: true)
    }
    
    open override func searchLink() {
        
    }
}
